package com.redis.publish;

import redis.clients.jedis.JedisPubSub;

/**
 * 发布和订阅的回调类
 */
public class MyJedisPubSub extends JedisPubSub {
	private String clientName;
	
	public MyJedisPubSub(String clientName) {
		this.clientName = clientName;
	}
	
	@Override
	public void onMessage(String channel, String message) {
		System.out.println(String.format("%s客户端, 订阅%s频道, 接受到的消息是: %s", clientName, channel, message));
	}
	
	@Override
	public void onSubscribe(String channel, int subscribedChannels) {
		System.out.println(String.format("%s客户端, 订阅%s频道, 一共订阅%d个频道", clientName, channel, subscribedChannels));
	}
	
	@Override
	public void unsubscribe(String... channels) {
		super.unsubscribe(channels); // 这个一定要有, 不然取消订阅不生效
		
		for (String item : channels) {
			System.out.println(String.format("%s客户端, 取消订阅%s频道", clientName, item));
		}
	}
	
	@Override
	public void onUnsubscribe(String channel, int subscribedChannels) {
		System.out.println(String.format("%s客户端, 取消订阅%s频道, 还订阅%d个频道", clientName, channel, subscribedChannels));
	}
	
}
